<template>
  <div>
    <n-modal
      v-if="props.showModal"
      v-model:show="props.showModal"
      :mask-closable="true"
      @update:show="$emit('CheckShowModal', false)"
    >
      <n-card
        style="width: 600px"
        title="添加用户"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
      >
        <template #header-extra>
          <n-button type="error" @click="$emit('CheckShowModal', false)">
            X
          </n-button>
        </template>
        <n-form ref="formRef" :model="model" :rules="rules">
          <n-form-item path="uName" label="用户名">
            <n-input v-model:value="model.uName" placeholder="请输入用户名" />
          </n-form-item>
          <n-form-item path="nLevell" label="密码">
            <n-input
              v-model:value="model.uPwd"
              type="password"
              placeholder="请输入密码"
            />
          </n-form-item>
          <n-form-item path="URL" label="电话">
            <n-input v-model:value="model.uTel" placeholder="请输入号码" />
          </n-form-item>
          <n-form-item path="URL" label="角色">
            <n-input-group>
              <n-select
                :style="{ width: '100%' }"
                :options="Options"
                placeholder="请选择角色"
                @change="handleChangeRlous"
              />
            </n-input-group>
          </n-form-item>
          <n-form-item path="URL" label="部门">
            <n-input-group>
              <n-select
                :style="{ width: '100%' }"
                :options="selectOptions"
                placeholder="请选择部门"
                @change="handleChangeDept"
              />
            </n-input-group>
          </n-form-item>
          <n-checkbox-group v-model:value="cities" @click="change">
            <n-space item-style="display: flex;">
              <n-checkbox
                v-for="item in menu"
                :key="item.nId"
                :value="item.nId"
                :label="item.nName"
              />
            </n-space>
          </n-checkbox-group>
          <n-row :gutter="[0, 24]">
            <n-col :span="24">
              <div style="display: flex; justify-content: center">
                <n-button round type="primary" @click="UpdateUserFile">
                  添加
                </n-button>
              </div>
            </n-col>
          </n-row>
        </n-form>
      </n-card>
    </n-modal>

    <n-skeleton v-else text :repeat="2" />
    <n-skeleton text style="width: 60%" />
  </div>
</template>

<script lang="ts" setup>
import { defineProps, ref, reactive } from "vue";
import { AddUser } from "@/api/user";
import { getNav } from "@/api/menu";
import { getRoles } from "@/api/rolse";
import { getOffice } from "@/api/office";
const formRef = ref();
const cities: any = ref(null);
const emit = defineEmits(["CheckShowModal", "reloadpage"]);
const props = defineProps({
  showModal: {
    type: Boolean,
    default: false,
  },
});
const params = reactive({});
const selectOptions: any = ref([]);
const Options: any = ref([]);
const model = reactive({
  uName: "",
  uPwd: "",
  uTel: "",
  rId: 0,
  dId: 0,
  uPower: "",
});
const menu: any = ref([]);
const rules = {
  uName: [
    {
      required: true,
      message: "请输入姓名",
      trigger: ["input", "blur"],
    },
  ],
  uPwd: [
    {
      required: true,
      message: "请输密码",
      trigger: ["input", "blur"],
    },
  ],
  uTel: [
    {
      required: true,
      message: "请输入号码",
      trigger: ["input", "blur"],
    },
  ],
};
getOffice(params).then((res) => {
  // console.log("部门", res);
  res.data.forEach((item: any) => {
    selectOptions.value.push({
      label: item.dName,
      value: item.dId,
    });
  });
});
getRoles().then((res) => {
  // console.log("角色", res);
  res.data.forEach((item: any) => {
    Options.value.push({
      label: item.rName,
      value: item.rId,
    });
  });
});
getNav(params).then((res) => {
  //   console.log(res);
  res.data.data.forEach((item: any) => {
    menu.value.push({ nId: item.nId, nName: item.nName });
  });
  // console.log(menu);
});
const handleChangeRlous = (value:any) => {
  // console.log("角色", value)
  model.rId=value
}
const handleChangeDept = (value:any) => {
  // console.log("部门", value)
  model.dId=value
}
const change = () => {
  // console.log(cities.value)
  model.uPower = cities.value.join(",");
  // console.log(model.uPower)
};
const UpdateUserFile = (e: Event) => {
  e.preventDefault();
  formRef.value.validate((err: any) => {
    if (err) {
      console.log(err);
    } else {
      AddUser(model).then((res: any) => {
        emit("CheckShowModal", false);
        emit("reloadpage");
      });
    }
  });
};
</script>

<style scoped lang="scss"></style>
